package com.gxa.hualian.service.impl;

import com.gxa.hualian.common.ExceptionCode;
import com.gxa.hualian.dao.UserDao;
import com.gxa.hualian.exception.BusinessException;
import com.gxa.hualian.mapper.UserMapper;
import com.gxa.hualian.pojo.User;
import com.gxa.hualian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Author ChenJin
 * @date 2021/10/18 10:36
 * Description:
 **/
@Service("userService")
@Transactional(rollbackFor = {Throwable.class})
public class UserServiceImpl implements UserService {
    // 通过注解给成员字段UserDao注入 @Autowired
    // 注入的适合 先判断类型（byType），
    // 如果类型相同就判断bean的名称byName:@Qualifier("userDao")
    // 如果要通过名字（userDao2），参数名称可以作为beanName的匹配依据
    // Autowired指出 字段/setter/构造方法。三种爪给你配手段
    @Autowired
//    @Resource
    private UserDao userDao = null;

    @Autowired
    private UserMapper userMapper = null;

/*    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }


    public UserServiceImpl() {
    }

    public UserServiceImpl(UserDao userDao){
        this.userDao = userDao;
    }*/



    @Override
    public void add() {
        System.out.println("UserServiceImpl add");
        userDao.add();
    }

//    @Transactional(rollbackFor = {Throwable.class})
//    @Override
//    public void delete(){
//         userMapper.deleteUser(14L);
//        int i = 1/0;
//        userMapper.deleteUser(23L);
//    }

    @Override
    public void test() {
        System.out.println("UserServiceImp test");
        int i = 1/0;
    }

    @Override
    public void test2(String name) {
        System.out.println("UserServiceImpl test2");
    }

    @Override
    public int test3(){
        int i =0;
        try{
            i++;
            return i;
        }finally {
            i++;
        }

    }

    @Override
    public User selectById(Long id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public int insert(User user) {
        user.setRegTime(new Timestamp(System.currentTimeMillis()));
        Integer integer = userMapper.addUser(user);
        return integer;
    }

    @Override
    public void delete(Long id) throws BusinessException {
        User user = userMapper.selectById(id);
        if (user!=null){
            userMapper.deleteUser(id);
        }else {
            throw new BusinessException(ExceptionCode.USER_WAS_NOT_FOUND);
        }
    }

    @Override
    public Boolean update(User user) throws BusinessException {
        User getUser = userMapper.selectById(user.getId());
        if (getUser!=null){
            userMapper.update(user);
        }else {
            throw new BusinessException(ExceptionCode.USER_WAS_NOT_FOUND);
        }
        return true;
    }

    @Override
    public List<User> selectAll() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    @Override
    public List<User> selectByLimit(Integer page, Integer size) {
        List<User> users = userMapper.selectByPage(page, size);
        return users;
    }

    @Override
    public List<User> selectBySearch(String value) {
        List<User> users = userMapper.selectBySearch(value);
        return users;
    }
}
